<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="70px">
        <el-form-item label="工厂">
          <el-select
            v-model="searchParam.plantId"
            placeholder="请输入工厂"
            clearable
            @keyup.enter="search"
            @change="closeDialog(searchParam.plantId)"
            filterable
          >
            <el-option
              v-for="item in plantList"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作中心">
          <el-select
            v-model="searchParam.workCenterId"
            placeholder="请选择工作中心"
            :disabled="searchParam.plantId === ''"
            clearable
            @keyup.enter="search"
            filterable
          >
            <el-option
              v-for="item in workshopList"
              :key="item.id"
              :label="`${item.workshopCode}-${item.workshopName}`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备编号">
          <el-input
            v-model="searchParam.equipCode"
            placeholder="请输入设备编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input
            v-model="searchParam.equipName"
            placeholder="请输入设备名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="设备类型">
          <el-tree-select
            placeholder="请输入设备类型"
            filterable
            v-model="searchParam.equipTypeId"
            :data="mdmequiptypeTree"
            node-key="id"
            clearable
            :props="{ label: 'allName', children: 'nodes' }"
            check-strictly
            :render-after-expand="false"
          />
        </el-form-item>

        <!-- <el-form-item label="设备状态">
          <el-select
            v-model="searchParam.equipStatus"
            placeholder="请输入设备状态"
            clearable
          >
            <el-option
              v-for="item in equipStatusTree"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="位置">
          <el-tree-select
            placeholder="请输入位置"
            filterable
            v-model="searchParam.locationId"
            :data="locationInfoTree"
            node-key="id"
            clearable
            :props="{ label: 'allName', children: 'nodes' }"
            check-strictly
            :render-after-expand="false"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <!-- <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        > -->
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="工厂"
          prop="plantCode"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="工作中心"
          prop="workCenterCode"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备编号"
          prop="equipCode"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备名称"
          prop="equipName"
          min-width="200"
          show-overflow-tooltip
        />
        <!-- <el-table-column
          label="设备型号"
          prop="equipModel"
          min-width="120"
          show-overflow-tooltip
        /> -->
        <el-table-column
          label="设备类型"
          prop="equipTypeName"
          min-width="140"
          show-overflow-tooltip
        />
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="140"
          show-overflow-tooltip
        />
        <el-table-column
          label="是否无人设备"
          prop="isUnmannedEquip"
          min-width="120"
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("YesNo", scope.row.isUnmannedEquip) }}
          </template>
        </el-table-column>
        <el-table-column
          label="启用状态"
          prop="enabled"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.enabled === 1">启用</el-tag>
            <el-tag type="info" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="设备状态"
          prop="equipStatusName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="位置"
          prop="allName"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="资产编码"
          prop="assetCode"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="启用状态"
          prop="enabled"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.enabled === 1">启用</el-tag>
            <el-tag type="info" v-else>停用</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="更新时间"
          prop="lastUpdateTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="更新人"
          prop="lastUpdatedUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template #default="scope">
            <!-- <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button> -->
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="编辑"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialog(scope.row)"
                ><i-ep-edit
              /></el-button>
            </el-tooltip>

            <el-tooltip
              :hide-after="0"
              effect="light"
              content="删除"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="handleTableDelete(scope.row.id)"
                ><i-ep-delete
              /></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mdmequipinfomes-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id: string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MDMMDMEQUIPINFOMES",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mdm/mdmequipinfo/queryByPages";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  equipCode: "",
  equipName: "",
  equipTypeId: "",
  equipStatus: "",
  locationId: "",
};

const deleteParam = {
  api: "/mdm/mdmequipinfo/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

// 获取位置信息下拉树
const locationInfoTree = ref([]);
const getLocationInfoTree = async () => {
  const { data } = await request({
    url: "/mdm/locationInfo/getTree",
    method: "POST",
    data: {
      enabled: 1,
    },
  });
  locationInfoTree.value = data;
};

// 获取设备类型下拉树
const mdmequiptypeTree = ref([]);
const getMdmequiptypeTree = async () => {
  const { data } = await request({
    url: "/mdm/mdmequiptype/getTree",
    method: "POST",
    data: {
      enabled: 1,
    },
  });
  mdmequiptypeTree.value = data;
  console.log(mdmequiptypeTree.value);
};

// // 设备类型选择
// const handleEquipTypeNameClick = (node: any) => {
//   searchParamInit.equipTypeName = node.equipTypeName;
//   searchParamInit.equipTypeCode = node.equipTypeCode;
//   console.log(node);
// };

const equipStatusTree = ref([]);

onMounted(() => {
  getTableList();
  getPlantList();
  getLocationInfoTree();
  getMdmequiptypeTree();
  equipStatusTree.value = useDictionary("EquipStatus");
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mdm/mdmequipinfo/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}
//工厂列表
const plantList = ref([]);
const getPlantList = async () => {
  const { data } = await request({
    url: "system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plantList.value = data;
  searchParam.value.plantId = plantList.value[0].id;
  closeDialog(searchParam.plantId);
};
function closeDialog(plantId: String) {
  getWorkshopList(plantId);
}
//工作中心
const workshopList = ref([]);
const getWorkshopList = async (plantId: String) => {
  const { data } = await request({
    url: "mes/mesworkshop/getList",
    method: "post",
    data: { plantId: plantId },
  });
  workshopList.value = data;
};
/** 打开设备信息表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改设备信息";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增设备信息";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开设备信息表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "设备信息详情";

  addDialog.data = roleObj;
  addDialog.visible = true;
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
</script>
<style scoped lang="scss">
.search-container {
  :deep(.el-form-item__label) {
    width: 70px !important;
  }
}
</style>
